//数据库操作对象
var DbOpt = require("../models/Dbopt");
var DbBase = require("../models/db/DbBase");
var url = require('url');
//站点配置
var AdminFunc = require("../models/db/adminFunc");
var settings = require("../models/db/settings");
//材料
var Ingredient = require("../models/Ingredient");
//材料类型
var IngredientType = require("../models/IngredientType");
//短id
var shortid = require('shortid');

var util=require('../base/Util');
var express = require('express');
var router = express.Router();

var pageSize=20;

// 查询页面
router.get("/", function(req, res) {
	var page = util.di(req.query.page,1);
	var typeId=req.query.typeId;
	var keywords = util.ds(req.query.ingredientName);
	var condition={};

	if(keywords!=""){
		condition["name"]=keywords;
	}
	if(shortid.isValid(typeId)){
		condition["type"]=typeId;
	}

	DbOpt.pagination(Ingredient,condition,page,pageSize,function(qinfo){
		res.render("ingredient/ingredient", {
			ingredients : qinfo.docs,
			moment : require("moment"),
			total: qinfo.totalItems,
			page:qinfo.currentPage,
			ingredientName:keywords,
			typeId:typeId,
			totalPage:qinfo.totalPage
		});
	});
});

// 更新初始页面
router.get("/editinit", function(req, res) {
	var id = req.query.id;
	DbOpt.findOne(Ingredient,id,function(ingredient){
		res.render("ingredient/ingredient_edit", {
			ingredient : ingredient
		});
	});
});

// 更新操作
router.post("/updateinit", function(req, res) {
	var id = req.body.id;
	var name = util.ds(req.body.ingredientname);
	var sx= util.getPinYinSx(name);
	var ingredientTypeId=  req.body.ingredientType;
	var obj_typeid =  ingredientTypeId;
	var updateData={};
	updateData["name"]=name;
	updateData["sx"]=sx;
	updateData["type"]=obj_typeid;
	DbOpt.updateOneByID(Ingredient,id,updateData,function(){
		res.redirect('/ingredient');
	})
});

// 添加页面初始化
router.get("/addinit", function(req, res) {
	res.render("ingredient/ingredient_add", {
	});
});


// 添加材料
router.post("/add", function(req, res) {
	var ingredientTypeId= req.body.ingredientType;
	var obj_id = ingredientTypeId;
	var name=util.ds(req.body.ingredientname);
	var sx= util.getPinYinSx(name);
	var addData = {
			"name":name,
			"sx":sx,
			"time":new Date(),
			"type":obj_id,
			"pricerecords":[]
	}
	DbOpt.addOne(Ingredient,addData,function(){
		res.redirect('/ingredient');
	});
});

//删除材料
router.get("/del", function(req, res) {
	var id = req.query.id;
	DbOpt.del(Ingredient,id,function(){
		res.redirect('/ingredient');
	});
});


/*
 * 新增材料类型
 */
router.post("/additype", function(req, res) {
	var itypename= util.ds(req.body.itypename);
	var addData = {
			"name":itypename,
			"time":new Date()
	}
	DbOpt.addOne(IngredientType,addData,function(){
		res.json({success:1});
	});
});

/*
 * 删除材料类型
 */
router.get("/delitype", function(req, res) {
	var id = req.query.id;
	DbOpt.del(IngredientType,id,function(){
		res.json({success:1});
	});
});

/*
 * 材料列表
 */
router.get("/itypelist", function(req, res) {
	DbOpt.findAll(IngredientType,function(ingredientTypes){
		res.json(ingredientTypes);
	})
});

module.exports = router;